﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BUS.DAL
{
    public class KhachHangDAL
    {
        internal static int Insert(KhachHangBUS khachHangBUS)
        {
            ShopDataContext db = DataAccess.Database;
            KhachHang customer = new KhachHang()
            {
                MaKhachHang = khachHangBUS.MaKhachHang,
                HoTen = khachHangBUS.HoTen,
                SoDienThoai = khachHangBUS.SoDienThoai,
                DiaChi = khachHangBUS.DiaChi,
                CMND = khachHangBUS.CMND,
                Email = khachHangBUS.Email
            };

            db.KhachHangs.InsertOnSubmit(customer);
            db.SubmitChanges();

            return customer.ID;
        }

        internal static string GetLastID()
        {
            ShopDataContext db = DataAccess.Database;

            var result = from p in db.KhachHangs
                         orderby p.ID descending
                         select p;


            //if(result.Count>0)
            return (result.Count() > 0 ? (string)result.FirstOrDefault<KhachHang>().MaKhachHang : "KH000");
        }

        internal static void Delete(DatHangBUS datHangBUS)
        {
            ShopDataContext db = DataAccess.Database;
            KhachHang kh = db.KhachHangs.SingleOrDefault<KhachHang>(p => p.ID == datHangBUS.KhachHang.ID);
            db.KhachHangs.DeleteOnSubmit(kh);
            db.SubmitChanges();
        }
    }
}
